package com.lpf.example.example07.client;

import com.lpf.example.example07.StuffInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

public class MyClientHandler extends SimpleChannelInboundHandler<StuffInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, StuffInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        int random = new Random().nextInt(3);

        StuffInfo.MyMessage message;

        if (0 == random) {
            message = StuffInfo.MyMessage.newBuilder()
                    .setDataType(StuffInfo.MyMessage.DataType.PersonType)
                    .setPerson(StuffInfo.Person.newBuilder().setName("张三").setAge(20).setAddress("北京").build())
                    .build();
        } else if (1 == random) {
            message = StuffInfo.MyMessage.newBuilder()
                    .setDataType(StuffInfo.MyMessage.DataType.DogType)
                    .setDog(StuffInfo.Dog.newBuilder().setName("dog").setAge(2).build())
                    .build();
        } else {
            message = StuffInfo.MyMessage.newBuilder()
                    .setDataType(StuffInfo.MyMessage.DataType.CatType)
                    .setCat(StuffInfo.Cat.newBuilder().setName("cat").setCity("天津").build())
                    .build();
        }

        ctx.writeAndFlush(message);
    }
}
